﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace PokeTD
{
    class BuyTableEntry
    {
        private Texture2D _buytexture;
        private string _Name;
        private int _towerid;
        private Vector2 _RenderRoot;
        private MouseState lastState = Mouse.GetState();
        private Rectangle PosRect;
        public event TowerBuyTableEntryEventHandler TowerBuyTableEntryClicked;
        private SpriteFont _font;
        private bool _chosen = false;
        private Texture2D _chosenframe;
        private int _preis;
        private double _range;

        public Texture2D Buytexture
        {
            get { return _buytexture; }
            set { _buytexture = value; }
        }
        public Texture2D Chosenframe
        {
            get { return _chosenframe; }
            set { _chosenframe = value; }
        }
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public int TowerID
        {
            get { return _towerid; }
            set { _towerid = value; }
        }
        public Vector2 RenderRoot
        {
            get { return _RenderRoot; }
            set { _RenderRoot = value; }
        }
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }
        public bool Chosen
        {
            get { return _chosen; }
            set { _chosen = value; }
        }
        public int Preis
        {
            get { return _preis; }
            set { _preis = value; }
        }
        public double Range
        {
            get { return _range; }
            set { _range = value; }
        }

        /// <summary>
        /// Erstellt eine neue BuyTableEntry
        /// </summary>
        /// <param name="Pic">Angezeigtes Bild in der Buytable</param>
        /// <param name="name">Name des Towers</param>
        /// <param name="TID">ID des zugehörigen Towers</param>
        /// <param name="renderroot">Vector2 zur linken Oberen Ecke des Objekts</param>+
        /// <param name="p">Preis des Towers</param>
        public BuyTableEntry(Texture2D Pic, string name, int TID, Vector2 renderroot, SpriteFont fnt, Texture2D frame, int p, double trange)
        {
            _buytexture = Pic;
            _Name = name;
            _towerid = TID;
            _RenderRoot = renderroot;
            PosRect = new Rectangle((int)_RenderRoot.X, (int)_RenderRoot.Y, 92,92);
            _font = fnt;
            _chosenframe = frame;
            _preis = p;
            _range = trange;
        }

        public void Update()
        {
            MouseState state = Mouse.GetState();

            if (lastState.LeftButton == ButtonState.Released && state.LeftButton == ButtonState.Pressed && (PosRect.Contains(new Point(state.X, state.Y)))) {
                //Click'ed.
                TowerBuyTableEntryClicked(this, new TowerBuyTableEntryEventArgs());
            }
            lastState = Mouse.GetState();
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(_buytexture, _RenderRoot, Color.White);
            sb.DrawString(_font, _Name, _RenderRoot + new Vector2(2, 90), Color.Black);
            if (_chosen)
                sb.Draw(_chosenframe, _RenderRoot - new Vector2(2, 2), Color.White);
        }
    }
}
